using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace StarlightEngine
{
    public class TextOnlyButtonNode : TextNode
    {
        Color noTouch = Color.White, touch = Color.DarkGray;
#if ZUNE
        bool touchedLast = false, touchedNow = false;
#endif
        public delegate void ButtonActivatedEvent(object o, EventArgs e);
        public ButtonActivatedEvent ButtonDown;

#if ZUNE
        bool firedLastRound = false;
#endif
        public TextOnlyButtonNode(string font, string text)
            : base(font)
        {
            
        }

        public override void Update()
        {
#if ZUNE
            touchedLast = touchedNow;

            if (this.Rectangle.Intersects(InputManager.Touch.FirstTouchRectangleTransformed))
            {
                Touched();
                touchedNow = true;
            }
            else
            {
                touchedNow = false;
            }

            if (!touchedNow)
            {
                //We're not.
                DrawColor = noTouch;

                //We were touched last frame, but we've let go now.
                if (touchedLast)
                {
                    //If there are no touches left.
                    if (InputManager.Touch.TotalTouches == 0)
                    {
                        //We were clicked last frame, its been let go and there are no touches.
                        //Should be here, but logic is wrong.
                        //this.ButtonDown(new object(), new EventArgs());
                    }
                }
            }
            else
            {
                //We're being touched.
                DrawColor = touch;
                if (!touchedLast && !firedLastRound)
                {
                    this.ButtonDown(new object(), new EventArgs());
                    firedLastRound = true;
                }
                else
                {
                    firedLastRound = false;                    
                }

            }
#endif
            base.Update();
        }

        public virtual void Touched()
        {
#if ZUNE
            touchedNow = true;
            //Let this be overrideen to do bidding when touched.
#endif
        }       
    }
}